<?php

namespace app\shop\controller\coupon;

use think\Controller;

class Coupon extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->BusinessModel = model('Business.Business');
        $this->ReceiveModel = model('Coupon.Receive');
        $this->CouponModel = model('Coupon.Coupon');

        $this->business = [];
        // 查询用户是否存在
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $this->business=$this->BusinessModel->find($busid);
            if(!$this->business)
            {
                $this->error('请先登录');
                exit;
            }
            // exit;
        }
    }
    
    public function index()
    {
        if($this->request->isPost())
        {
            $coupon=$this->CouponModel->where(['status' => '1'])->select();

            if(!$coupon)
            {
                $this->error('暂无优惠劵');
                exit;
                
            }

            // 查询用户是否领取过优惠劵
            $where=[
                'busid'=>$this->business->id,
                'status'=> ['<>','-1']
            ];
            // 领取列表
            $receive=$this->ReceiveModel->where($where)->column('cid');
            foreach($coupon as $key => $item)
            {
                $coupon[$key]['receive'] = in_array($item['id'],$receive) ? true :false;
            }

            $this->success('优惠劵列表',null,$coupon);
            exit;
        }
    }

    
}